package ewu

import java.util.ArrayList
import scala.collection.mutable.ArrayBuffer
import ewu._

/**
 * Die AND-Klasse stellt die logische UND-Verknuepfung dar.
 * Sind die beiden boolschen Ausdruecke "left" sowie "right"
 * True, so ergibt sich nach Auswertung ein True.
 * Beispiel: x AND y
 */

case class AND (left: BoolExpression, right: BoolExpression) extends BoolExpression {
  
  def evaluate (env: Environment): Option[Boolean] =
    (left.evaluate(env), right.evaluate(env)) match {
      case (Some(le), Some(ri)) => Some(le && ri)
      case _ => None
  }

  override def toString: String = left + " AND " + right

  def reduce: BoolExpression = (left.reduce, right.reduce) match {
    case (le, ri) if le.isTrue && ri.isTrue => new True
    case (le, ri) if le.isTrue => ri
    case (le, ri) if ri.isTrue => le
    case (le, ri) => AND(le, ri)
  }

  def isTrue: Boolean = left.isTrue && right.isTrue
  
  def getVariables(a: ArrayBuffer[Name]) = {
    left.getVariables(a)
    right.getVariables(a)
  }
  
  override def replaceQuantor(addfuncs: ArrayList[String], arrayvars: ArrayList[String]) : BoolExpression = {
    return new AND(left.replaceQuantor(addfuncs, arrayvars),right.replaceQuantor(addfuncs, arrayvars))
  }
}


 